package tool;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.MalformedURLException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.mengtian_tech.smart_lab.entity.apply.ApplyForm;

public class PDFTool {
	
	public static File generateToFile(ApplyForm form,String fileName) throws Exception{
		File file = File.createTempFile(fileName+"-"+System.currentTimeMillis()+"", ".pdf");
		generate(new FileOutputStream(file), form);
		return file;
	}
	
	public static  void generate(OutputStream os,ApplyForm applyForm) throws DocumentException, MalformedURLException, IOException{
		//Step 1—Create a Document.  
		Document document = new Document();  
		//Step 2—Get a PdfWriter instance.  
		PdfWriter.getInstance(document, os);  
		//Step 3—Open the Document.  
		document.open();  
		//Step 4—Add content.  
		
		PdfPTable table = new PdfPTable(5);
		table.addCell(getPDFCell("日期"));  
		PdfPCell cell = new PdfPCell(new Phrase("2017/07-2017/09"));  
		cell.setColspan(4);  
		table.addCell(cell);
		
		table.addCell("");  
		table.addCell(getPDFCell("数额"));  
		table.addCell(getPDFCell("增值税"));  
		table.addCell(getPDFCell("净额"));
		table.addCell(getPDFCell("备注"));
		
		table.addCell(getPDFCell("销售额"));  
		table.addCell(applyForm.getSellAmount().toString());  
		table.addCell(applyForm.getSellAmountTax().toString());  
		table.addCell(applyForm.getSellAmount().subtract(applyForm.getSellAmountTax()).toString());
		table.addCell("");
		
		table.addCell(getPDFCell("开支"));  
		table.addCell(applyForm.getSpendAmount().toString());  
		table.addCell(applyForm.getSpendAmountTax().toString());  
		table.addCell(applyForm.getSpendAmount().subtract(applyForm.getSpendAmountTax()).toString());
		table.addCell("");
		
		table.addCell(getPDFCell("利润"));  
		table.addCell(applyForm.getBalance().toString());
		BigDecimal balanceTax = applyForm.getBalance().divide(new BigDecimal(11),RoundingMode.HALF_DOWN);
		table.addCell(balanceTax.toString());  
		table.addCell(applyForm.getBalance().subtract(balanceTax).toString());
		table.addCell("");
		  
		if(applyForm.getAttachData() != null){
			table.addCell(getPDFCell("签名"));
			Image image = Image.getInstance(applyForm.getAttachData());
			float heigth=image.getHeight();  
	        float width=image.getWidth();
	        image.scaleAbsoluteHeight(70);
	        image.scaleAbsoluteWidth(180);
			cell = new PdfPCell(image);
			cell.setColspan(4);
			table.addCell(cell);
		}
		
		document.add(table);
		document.close();  
	}
	
	private static  PdfPCell getPDFCell(String text) throws DocumentException, IOException{
		//BaseFont chinese = BaseFont.createFont(PDFTool.class.getResource("/") + "/simsunb.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		BaseFont chinese = BaseFont.createFont( "STSongStd-Light" ,"UniGB-UCS2-H",false );  
		return  new PdfPCell(new Phrase(text,new Font(chinese)));
	}
	
	public static void main(String args[]) throws MalformedURLException, FileNotFoundException, DocumentException, IOException{
		ApplyForm applyForm = new ApplyForm();
		applyForm.setSellAmount(new BigDecimal(100));
		applyForm.setSellAmountTax(new BigDecimal("3"));
		applyForm.setSpendAmount(new BigDecimal("3333"));
		applyForm.setSpendAmountTax(new BigDecimal("1"));
		applyForm.setBalance(new BigDecimal("3232"));
		
		applyForm.setAttachData(new byte[]{});
		
		PDFTool.generate(new FileOutputStream("e:/test/test.pdf"), applyForm);
	}
}
